package com.gx.telephonedemo2;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;

import java.io.File;

/**
 * Description:
 * Author: GX
 * CreateDate: 2019/8/14 19:45
 * UpdateUser: GX
 * UpdateDate: 2019/8/14 19:45
 * UpdateRemark:
 * Version: 1.0
 */
public class PhoneStateReceiver extends BroadcastReceiver {
    private final String TAG = "PhoneService";
    private boolean recordState = false;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (System.currentTimeMillis() - Constant.guaduanTime < 1000) {
            return;
        }
        Log.i("BlockCallHelper", "BlockCallHelper------->>>>启动");

        BlockCallHelper blockCallHelper = BlockCallHelper.getInstance();
        blockCallHelper.init(context);
        blockCallHelper.injectBlockPhoneNum(Constant.blockCalls);
        blockCallHelper.setBlockCallBack(new BlockCallHelper.BlockCallBack() {
            @Override
            public void callBack(String incomingNum) {
                Log.i("BlockCallHelper", "incomingNum-----------" + incomingNum);
                EventBus.getDefault().post(new MessageEvent(incomingNum));
            }

            @Override
            public void startRecord() {
                if (recordState) {
                    return;
                }
                Log.d(TAG, "(测试)开始录音...");
                onRecord(context, true);
            }

            @Override
            public void stopRecord() {
                Log.d(TAG, "(测试)录音结束...");
                onRecord(context,false);
            }
        });
        if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
            String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
            Log.i("BlockCallHelper", "BlockCallHelper------->>>>" + phoneNumber);
            Constant.number = phoneNumber;
            Constant.isHujiao = true;
            Constant.isGuaduan = false;
            Constant.isZhujiaoZhaiji = true;
        }
    }

    private void onRecord(Context context, boolean start) {
        Intent intent = new Intent(context, RecordingService.class);
        if (start) {
            // start recording
            recordState = true;
            Toast.makeText(context, "开始录音...", Toast.LENGTH_SHORT).show();
            File folder = new File(Environment.getExternalStorageDirectory() + "/CallAutoRecord");
            if (!folder.exists()) {
                //folder /SoundRecorder doesn't exist, create the folder
                folder.mkdir();
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                context.startForegroundService(intent);
            }else {
                context.startService(intent);
            }
        } else {
            //stop recording
            Toast.makeText(context, "录音结束...", Toast.LENGTH_SHORT).show();
            context.stopService(intent);
            recordState = false;
        }
    }
}
